﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Avatar.Model;
using AvatarHelper;
namespace Avatar.ResumeService
{
    public static class BasicInfoManager
    {
        public static void BasicInfoSave(
            string _user,
            int _accountLocation,
            DateTime _birthday,
            string _blog,
            string _documentNumber,
            int _documentTypeID,
            int _height,
            string _keyWord,
            int _livingCity,
            int _marry,
            string _realName,
            string _qq,
            int _salary,
            string _telNumber,
            int _workingAgeID,
            int _diplomaID,
            int _sexID)
        {
            try
            {
                #region 入口检测

                if (string.IsNullOrEmpty(_user)) throw new Exception("登录超时 请重新登录");
                if (_user.Length > 50) throw new Exception("当前用户名过长");
                if (!string.IsNullOrEmpty(_blog) && _blog.Length > 80) throw new Exception("Blog地址不能超过80个字");
                //if (string.IsNullOrEmpty(_documentNumber)) throw new Exception("证件号码不能为空");
                if (!string.IsNullOrEmpty(_documentNumber) && _documentNumber.Length > 18) throw new Exception("证件号码不能超过15个字符");
                if (!string.IsNullOrEmpty(_keyWord) && _keyWord.Length > 70) throw new Exception("关键字不能超过70个字符");
                if (string.IsNullOrEmpty(_realName)) throw new Exception("姓名不能为空");
                if (_realName.Length > 15) throw new Exception("姓名不能超过15个字");
                if (!string.IsNullOrEmpty(_qq) && _qq.Length > 12) throw new Exception("QQ号码不能超过12个字符");
                if (string.IsNullOrEmpty(_telNumber)) throw new Exception("电话号码不能为空");
                if (_telNumber.Length > 16) throw new Exception("电话号码不能超过16个字符");

                #endregion
                using (var _adc = DBHelper.GetDataContext())
                {
                    var _resume = _adc.Resume.Where(r => r.User == _user).FirstOrDefault();
                    if (_resume != null)
                    {
                        if (_resume.PersonalBasicInfo == null)
                        {
                            _resume.PersonalBasicInfo = new PersonalBasicInfo();
                            _resume.PersonalBasicInfo.AccountLocation = _accountLocation;
                            _resume.PersonalBasicInfo.Birthday = _birthday;
                            _resume.PersonalBasicInfo.Blog = _blog;
                            _resume.PersonalBasicInfo.DocumentNumber = string.IsNullOrEmpty(_documentNumber) ? string.Empty : _documentNumber;
                            _resume.PersonalBasicInfo.DocumentTypeID = _documentTypeID;
                            _resume.PersonalBasicInfo.Email = _user;
                            _resume.PersonalBasicInfo.Height = _height;
                            _resume.PersonalBasicInfo.KeyWord = _keyWord;
                            _resume.PersonalBasicInfo.LivingCity = _livingCity;
                            _resume.PersonalBasicInfo.MaritalStatusID = _marry;
                            _resume.PersonalBasicInfo.Name = _realName;
                            _resume.PersonalBasicInfo.QQ = _qq;
                            _resume.PersonalBasicInfo.SalaryID = _salary;
                            _resume.PersonalBasicInfo.TelNumber = _telNumber;
                            _resume.PersonalBasicInfo.WorkingAgeID = _workingAgeID;
                            _resume.PersonalBasicInfo.DiplomaID = _diplomaID;
                            _resume.PersonalBasicInfo.SexID = _sexID;
                            _resume.PersonalBasicInfo.ID = Guid.NewGuid();

                            _adc.PersonalBasicInfo.InsertOnSubmit(_resume.PersonalBasicInfo);
                        }
                        else
                        {
                            _resume.PersonalBasicInfo.AccountLocation = _accountLocation;
                            _resume.PersonalBasicInfo.Birthday = _birthday;
                            _resume.PersonalBasicInfo.Blog = _blog;
                            _resume.PersonalBasicInfo.DocumentNumber = string.IsNullOrEmpty(_documentNumber) ? string.Empty : _documentNumber; ;
                            _resume.PersonalBasicInfo.DocumentTypeID = _documentTypeID;
                            _resume.PersonalBasicInfo.Height = _height;
                            _resume.PersonalBasicInfo.KeyWord = _keyWord;
                            _resume.PersonalBasicInfo.LivingCity = _livingCity;
                            _resume.PersonalBasicInfo.MaritalStatusID = _marry;
                            _resume.PersonalBasicInfo.Name = _realName;
                            _resume.PersonalBasicInfo.QQ = _qq;
                            _resume.PersonalBasicInfo.SalaryID = _salary;
                            _resume.PersonalBasicInfo.TelNumber = _telNumber;
                            _resume.PersonalBasicInfo.WorkingAgeID = _workingAgeID;
                            _resume.PersonalBasicInfo.DiplomaID = _diplomaID;
                            _resume.PersonalBasicInfo.SexID = _sexID;
                        }
                        _resume.UpdateDate = DateTime.Now;
                        _adc.SubmitChanges();
                    }
                }
            }
            catch (Exception _e)
            {
                throw new Exception("基本资料保存出错:" + _e.Message);
            }
        }
    }
}
